package com.ychug.framework.core.advice;

import com.ychug.common.utils.DateUtils;
import com.ychug.framework.business.dto.CmsNewsDTO;
import com.ychug.framework.core.tag.TagBase;
import com.ychug.service.cms.entity.CmsNewsDataEntity;
import com.ychug.service.cms.entity.CmsNewsEntity;
import com.ychug.service.cms.service.ICmsNewsDataService;
import com.ychug.service.cms.service.ICmsNewsService;
import com.ychug.service.system.entity.SysConfigEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class NewsDataListDirective extends TagBase {

    @Autowired
    private ICmsNewsService cmsNewsService;

    @Autowired
    private ICmsNewsDataService cmsNewsDataService;

    @Override
    public void onRender() {
        Map<String,Object> parmas = new HashMap<String,Object>();

        //如果action=list 显示列表  action = detail 显示详情

        String tAction = getParam("action");
        String tSitId = getParam("sitId");
        String tContentId = getParam("contentId");
        String tCateId = getParam("cateId");

        System.out.println(" tAction: "+tAction +" -- tCateId : " + tCateId + "--tContentId: " + tContentId );

        CmsNewsEntity cmsNewsEntity = new CmsNewsEntity();
        if(tAction != null && tAction.equals("list")){
            cmsNewsEntity.setCatId(Integer.parseInt(getParam("cateId")));
            List<CmsNewsDTO> CmsNewsDTOList = getCmsNewsDataList(cmsNewsEntity);
            setVariable("newsList", CmsNewsDTOList);

        }else if(tAction != null && tAction.equals("detail")){
            cmsNewsEntity.setContentId(Integer.parseInt(getParam("contentId")));
            List<CmsNewsDTO> CmsNewsDTOList = getCmsNewsDataList(cmsNewsEntity);
            setVariable("newsList", CmsNewsDTOList);
        }

        renderBody();
    }

    /**
     * 获取后台中的数据
     * @param cmsNewsData
     * @return
     */
    private List<CmsNewsDTO> getCmsNewsDataList(CmsNewsEntity cmsNewsData){
        List<CmsNewsDTO> list = new ArrayList<>();

        //查询文档主表中的数据
        List<CmsNewsEntity> cmsNewsEntityList = cmsNewsService.selectCmsNewsList(cmsNewsData);
        for(CmsNewsEntity cmsNews : cmsNewsEntityList){
            //查询从表中的数据
            CmsNewsDataEntity cmsNewsDataEntity = new CmsNewsDataEntity();
            cmsNewsDataEntity.setContentId(Integer.parseInt(cmsNews.getContentCode()));
            List cmsNewsDataEntityList =  cmsNewsDataService.selectCmsNewsDataList(cmsNewsDataEntity);
            cmsNewsDataEntity = (CmsNewsDataEntity) cmsNewsDataEntityList.get(0);

            CmsNewsDTO cmsNewsDTO = new CmsNewsDTO();
            cmsNewsDTO.setContentId(cmsNews.getContentId());
            cmsNewsDTO.setSiteId(cmsNews.getSiteId());
            cmsNewsDTO.setContentCode(cmsNews.getContentCode());
            cmsNewsDTO.setCatId(cmsNews.getCatId());
            cmsNewsDTO.setCatName(cmsNews.getCatName());

            cmsNewsDTO.setPublishDate(DateUtils.parseDateToStr("yyyy-MM-dd",cmsNews.getPublishDate()));
            cmsNewsDTO.setOffDate(DateUtils.parseDateToStr("yyyy-MM-dd",cmsNews.getOffDate()));
            cmsNewsDTO.setPriority(cmsNews.getPriority());
            cmsNewsDTO.setStatus(String.valueOf(cmsNews.getStatus()));
            cmsNewsDTO.setCheckStatus(cmsNews.getCheckStatus());

            cmsNewsDTO.setTitle("["+cmsNews.getCatName()+"] "+cmsNewsDataEntity.getTitle());
            cmsNewsDTO.setFullTitle(cmsNewsDataEntity.getFullTitle());
            cmsNewsDTO.setLink(cmsNewsDataEntity.getLink());
            cmsNewsDTO.setIsNewWindow(cmsNewsDataEntity.getIsNewWindow());
            cmsNewsDTO.setColor(cmsNewsDataEntity.getColor());
            cmsNewsDTO.setNewsPath(cmsNewsDataEntity.getNewsPath());
            cmsNewsDTO.setNewsTemplate(cmsNewsDataEntity.getNewsTemplate());
            cmsNewsDTO.setDescription(cmsNewsDataEntity.getDescription());
            cmsNewsDTO.setSource(cmsNewsDataEntity.getSource());
            cmsNewsDTO.setSourceUrl(cmsNewsDataEntity.getSourceUrl());
            cmsNewsDTO.setContent(cmsNewsDataEntity.getContent());
            cmsNewsDTO.setAuthor(cmsNewsDataEntity.getAuthor());
            list.add(cmsNewsDTO);
        }
        return list;

    }

}
